import { createRouter, createWebHistory, type RouteRecordRaw } from "vue-router";
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

import HomeView from "../views/home/IndexView.vue";
import NotFound from "@/components/NotFound.vue";


import AppLayout from '@/components/layouts/AppLayout.vue'
import productRoutes from './modules/product'
import store from "@/stores";
// import orderRoutes from './modules/order'
// import permissionRoutes from './modules/permission'
// import mediaRoutes from './modules/media'



const routes:RouteRecordRaw[] = [
  {
    path: "/",
    name: "home",
    component: AppLayout,
    children: [
      {
        path: "/",
        name: "home",
        component: HomeView,
      },
      productRoutes,
      // orderRoutes,
      // permissionRoutes,
      // mediaRoutes
    ],
  },
  {
    path: "/login",
    name: "login",
    // route level code-splitting
    // this generates a separate chunk (About.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import("../views/login/IndexView.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: NotFound,
    meta: { requiresAuth: false },
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes ,
});

nprogress.configure({})
router.beforeEach((to) => {
  nprogress.start()
  //requiresAuth
  if(to.meta.requiresAuth && !store.state.user){
    return {
      name: 'login',
      query: {
        redirect: to.fullPath
      }
    }
  }
})

router.afterEach(() => {
  nprogress.done()
})

export default router;
